package com.th.supcom.pds.auditprescription.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.auditprescription.client.model.RefuseReasonModel;
import com.th.supcom.pds.auditprescription.client.service.AuditPrescripRpcAsync;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.client.model.PdsSupplyModel;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.enums.CheckFlagEnum;
import com.th.supcom.pds.common.utils.ClientUtils;

/**
 * @author yzhang
 */
public class AuditPrescripAction
{
    private static final AuditPrescripRpcAsync S_ASYNC = PdsAuditPrescServiceFactory.getAuditPrescripService ();
    private AuditPrescripMainPanel m_auditPrescripMainPanel;
    private AuditPrescripSearchPanel m_auditPrescripSearchPanel;

    /**
     * 添加快捷键事件
     */
    private void addEnterKeyListener ()
    {
        addWindowKeyListener ();
        // 通过按钮
        ClientUtils.getFocusChange (m_auditPrescripSearchPanel.getQueryBtn (), m_auditPrescripSearchPanel.getRefuseBtn (),
                                    m_auditPrescripSearchPanel.getPassBtn ());
        // 查询按钮
        ClientUtils.getFocusChange (m_auditPrescripSearchPanel.getPrescription (), m_auditPrescripSearchPanel.getPassBtn (),
                                    m_auditPrescripSearchPanel.getQueryBtn ());
        // 拒绝按钮
        ClientUtils.getFocusChange (m_auditPrescripSearchPanel.getPassBtn (), m_auditPrescripSearchPanel.getClearBtn (),
                                    m_auditPrescripSearchPanel.getRefuseBtn ());
        // 清除按钮
        ClientUtils.getFocusChange (m_auditPrescripSearchPanel.getRefuseBtn (), null, m_auditPrescripSearchPanel.getClearBtn ());
        new KeyNav <ComponentEvent> (m_auditPrescripMainPanel)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                if (ce.isAltKey ())
                {
                    combinationkey (ce.getKeyCode ());
                }
            }
        };
    }

    private void addWindowKeyListener ()
    {
        final Dialog window = m_auditPrescripSearchPanel.getRefuseWindow ();
        final Button yesBtn = window.getButtonById (Dialog.YES);
        final Button noBtn = window.getButtonById (Dialog.NO);
        window.setFocusWidget (yesBtn);
        // YES按钮绑定右方向键
        ClientUtils.getFocusChange (null, noBtn, yesBtn);
        // NO按钮绑定左方向键
        ClientUtils.getFocusChange (yesBtn, null, noBtn);
        final ComboBox <RefuseReasonModel> comboBox = m_auditPrescripSearchPanel.getComboBox ();
        final TextArea textArea = m_auditPrescripSearchPanel.getTextArea ();
        comboBox.setValue (m_auditPrescripSearchPanel.getLists ().get (0));

        textArea.clear ();

        window.getButtonById (Dialog.NO).addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                window.hide ();
                m_auditPrescripSearchPanel.getPrescription ().focus ();
            }
        });
        window.getButtonById (Dialog.YES).addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                String message;
                if (textArea.getValue () == null)
                {
                    message = comboBox.getValue ().getName ();
                }
                else
                {
                    message = comboBox.getValue ().getName () + textArea.getValue ();
                }
                
                final String operator = ClientUtils.geteEmpModel().getEmpCode();
                PdsSupplyModel pdsSupplyModel = new PdsSupplyModel ();
                pdsSupplyModel.setCheckMemo (message);
                pdsSupplyModel.setPrescId (22L);
                pdsSupplyModel.setUserIdCheck (operator);
                pdsSupplyModel.setUserNameCheck (operator);
                pdsSupplyModel.setOrgIdExec (ClientUtils.getOrgModel().getOrgCode());
                pdsSupplyModel.setOrgIdExecName (ClientUtils.getOrgModel().getOrgName());
                S_ASYNC.checkPresc (pdsSupplyModel, CheckFlagEnum.REFUSE.getEnumCode (), new AsyncCallback <String>()
                {

                    @Override
                    public void onFailure (Throwable caught)
                    {
                        MessageBox.alert ("警告", caught.getMessage (), null);
                    }

                    @Override
                    public void onSuccess (String result)
                    {
                        window.hide ();
                        m_auditPrescripMainPanel.clearInfo (true);
                        Info.display ("提示", result);
                        return;
                    }
                });
            }
        });
    }

    private void combinationkey (final int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            final PdsSupplyModel model = new PdsSupplyModel ();
            model.setPrescNo (m_auditPrescripSearchPanel.getPrescription ().getValue ());
            m_auditPrescripMainPanel.queryInfo (model);
        }
        if (keyCode == KeysConstants.VK_O)
        {
            m_auditPrescripMainPanel.passPresc ();
        }
        if (keyCode == KeysConstants.VK_N)
        {
            m_auditPrescripMainPanel.refusePresc ();
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_auditPrescripMainPanel.clearInfo (true);
        }
    }

    public void initAction ()
    {
        addEnterKeyListener ();
    }

    public void setAuditPrescripMainPanel (final AuditPrescripMainPanel auditPrescripMainPanel)
    {
        m_auditPrescripMainPanel = auditPrescripMainPanel;
    }

    public void setAuditPrescripSearchPanel (final AuditPrescripSearchPanel auditPrescripSearchPanel)
    {
        m_auditPrescripSearchPanel = auditPrescripSearchPanel;
    }

}
